package com.yanqu.road.server.cmd.activity.drink;

import com.yanqu.road.entity.activity.drink.DrinkUserData;
import com.yanqu.road.entity.activity.drink.config.DrinkConfig;
import com.yanqu.road.pb.activity.DrinkActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.drink.DrinkModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.drink.DrinkMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_DRINK_USE_ENERGY_ITEM, desc = "使用体力药")
public class DrinkUseEnergyItemCmd extends AbstractCommand {

    private int count = 0;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DrinkActivityProto.DrinkUseEnergyItemReqMsg reqMsg = DrinkActivityProto.DrinkUseEnergyItemReqMsg.parseFrom(packet.getBytes());
        count = reqMsg.getCount();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        DrinkConfig drinkConfig = DrinkMgr.getDrinkConfig();
        if (drinkConfig == null || !ActivityMgr.activityInTime(drinkConfig.getActivityInfo())) {
            ret = GameErrorCode.E_DRINK_ACTIVITY_NO_OPEN;
            return;
        }

        DrinkModule module = player.getModule(DrinkModule.class);
        DrinkUserData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_DRINK_ACTIVITY_NO_UNLOCK;
            return;
        }

        ret = module.useEnergyItem(count);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DrinkActivityProto.DrinkUseEnergyItemRespMsg.Builder builder = DrinkActivityProto.DrinkUseEnergyItemRespMsg.newBuilder();
            builder.setRet(ret);
            builder.setEnergy(0);
            builder.setLastRecTime(0);
            player.sendPacket(ClientProtocol.U_DRINK_USE_ENERGY_ITEM, builder);
        }
    }
}
